package com.unitech.drinks.controller;

import com.github.pagehelper.PageInfo;
import com.unitech.drinks.controller.param.PageParam;
import com.unitech.drinks.model.Category;
import com.unitech.drinks.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * CRUD案例
 */
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping(path = "/category")
    @ResponseStatus(code = HttpStatus.OK)
    public PageInfo<Category> listCategories(@RequestBody @Valid PageParam pager) {
        return categoryService.listCategories(pager.getPage(), pager.getSize());
    }

    @GetMapping(path = "/category/{id}")
    @ResponseStatus(code = HttpStatus.OK)
    public Category showCategory(@PathVariable Integer id) {
        return categoryService.showCategory(id);
    }

    @PostMapping(path = "/category/")
    public Category createCategory(@RequestBody @Valid Category category) {
        return categoryService.createCategory(category);
    }

    @PutMapping(path = "/category/")
    public Category modifyCategory(@RequestBody @Valid Category category) {
        return categoryService.modifyCategory(category);
    }

    @DeleteMapping(path = "/category/{id}")
    public void deleteCategory(@PathVariable Integer id ) {
        categoryService.deleteCategory(id);
    }

}
